<?php
namespace app\common\taglib;
use app\common\model\Content;
use think\template\TagLib;
use app\common\model\Article;
class Micuer extends TagLib
{

    protected $tags = [
        'articles' => ['attr'=> 'field,id,limit,cid,order,returnname', 'close'=>1],
        'contentlist' => ['attr'=> 'limit,cid,order,id', 'close'=>1]
    ];

    public function tagContentList($tag, $content){
        $id = "vo";
        $res = Content::where(1)->limit(1)->order("id DESC")->select()->toArray();
        $parse = <<<parse
{volist name="{$res}" id="{$id}"}
{$content}
{/volist}
parse;
        p($parse);die;
        return $parse;
    }
    /**
     * 文章标签
     */

    public function tagArticles($tag, $content)
    {
        p($tag);die;
        $id = $tag['id']?$tag['id']:'vo';

        $order         = empty($tag['order']) ? "'id DESC'" : '"'.$tag['order'].'"';
        $returnname = 'article_data';
        $field = "''";
        if (!empty($tag['field'])) {
            if (strpos($tag['field'], '$') === 0) {
                $field = $tag['field'];
                $this->autoBuildVar($field);
            } else {
                $field = "'{$tag['field']}'";
            }
        }
        $cid = "''";
        if (!empty($tag['cid'])) {
            if (strpos($tag['cid'], '$') === 0) {
                $cid = $tag['cid'];
                $this->autoBuildVar($cid);
            } else {
                $cid = "'{$tag['cid']}'";
            }
        }
        $limit = '';
        if (!empty($tag['limit'])) {
            if (strpos($tag['limit'], '$') === 0) {
                $limit = $tag['limit'];
                $this->autoBuildVar($limit);
            } else {
                $limit = "{$tag['limit']}";
            }
        }else{
            $limit = '0,5';
        }


        $params = [
            'field'   => $field,
            'cid'=>$cid,
            'limit'=>$limit,
            'order'=>$order
        ];
        //p($params);die;
        $returnname = $this->taglib_articles($params);
        $parse = <<<parse
{volist name="{$returnname}" id="{$id}"}
{$content}
{/volist}
parse;
        return $parse;
    }



    public function taglib_articles($param)
    {
        $field = $param['field'];
        $cid = $param['cid'];
        $limit = $param['limit'];
        $order = $param['order'];
        $result = Article::where('cid', 'in',$cid)->field($field)->limit($limit)->order($order)->select();
        return $result;
    }


}